<?php

class site_menuModel extends Model {

    public $tabela = "site_menu";
    public $pkey   = "cod_menu";
    public $dados  = array(
        
        'cod_menu' => array(
            'name'    => "Código",
            'pkey'    => true,
            'ai'      => true,
            'type'    => 'int',
            'size'    => '11',
            'grid'    => true,
            'notnull' => true,
            'display' => true,
            'private' => true,
         ),
        
        'menu' => array(
            'name'    => 'Nome',
            'type'    => 'varchar',
            'size'    => '20', 
            'grid'    => true,
            'notnull' => true,
            'display' => true,
       	),
        
        'pai' => array(
            'name'    => 'Pai',
            'grid'    => true,
            'fkey'    => array(
                'model'         => 'site/menu',
                'cardinalidade' => '1n', //nn 1n 11
                'keys'          => array('menuid', 'menu'),
                'onupdate'      => 'cascade',
                'ondelete'      => 'cascade'
            ),
            'display' => true,
        ),
 
        'menuid' => array(
            'name'    => 'Id',
            'type'    => 'varchar',
            'unique' => array('model'=>'site/menu'),
            'size'    => '20', 
            'grid'    => true,
            'notnull' => true,
            'display' => true,
         ),
         
        'url' => array(
            'name' => 'Url',
            'type' => 'varchar',
            'size' => '128',
            'notnull' => 'true',
            'grid' => true,
            'display' => true,
         ),
        
         'ordem' => array(
            'name'  => 'Ordem',
            'type'  => 'int',
            'size'  => '5',
            'grid'  => true,
            'display' => true,
         )
        
    );
    
    public function __construct() {
        $this->dados['plugin']['listfolder']['folder']  = MODULOS;
        parent::__construct();
    }

    public function inserir($dados) {
        cookie::destroy($this->cookiename);
        return parent::inserir($dados);
    }
    
    public function editar($id, $post, $camp = "") {
        cookie::destroy($this->cookiename);
        return parent::editar($id, $post, $camp);
    }
    
    private $cookiename = 'menu_superior';
     public function getMenu(){
        $this->LoadComponent('notificacao/notifica', 'not');
        $this->not->ajaxCheck();
        
        $this->LoadModel('usuario/login', 'uobj');
        $is = $this->uobj->UserIsWebmaster();
        if(!$is){
            if(cookie::cookieExists($this->cookiename)){
                $var = cookie::getVar($this->cookiename);
                if(!empty($var)) return $var;
            }
        }
        $menu_db = $this->selecionar(array('menu', 'url', 'menuid', 'pai'), '', '','', "ordem ASC");
        $mn = new menuBuilder($menu_db);
        $mn->setNomePai('pai');
        $mn->setMenuId('menuid');
        $var = $mn->geraMenu();
        if(!$is) cookie::setVar($this->cookiename, $var);
        return $var;
    }
    
    public function paginate($page, $link = "", $cod_item = "", $campo = "", $qtd = 20, $campos = array(), $adwhere = "", $order = "") {
        $order = "site_menu_1.menu ASC, site_menu.menu ASC";
        return parent::paginate($page, $link, $cod_item, $campo, $qtd, $campos, $adwhere, $order);
    }
    
    public function unstall($plugin){
        $where = "url LIKE '$plugin/%'";
        if(!$this->db->Delete($this->tabela, $where)){
            $this->setErrorMessage($this->db->getErrorMessage());
            return false;
        }
        return true;
    }

}

?>
